iOS 10前后推送的区别

处理远程推送消息的几种情况

app在前台时

在 iOS9 下调用:

1
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo

在 iOS10 下调用:

1
2
3
4
5
6
7
8
9
10
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{
NSDictionary * userInfo = notification.request.content.userInfo;
if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
//应用处于前台时的远程推送接受
}else{
//应用处于前台时的本地推送接受
}
//当应用处于前台时提示设置,需要哪个可以设置哪一个
completionHandler(UNNotificationPresentationOptionSound|UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionAlert);
}

app在后台时

在 iOS9 下调用:

1
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo

在 iOS10 下调用:

1
2
3
4
5
6
7
8
9
//iOS10新增:处理后台点击通知的代理方法
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{
NSDictionary * userInfo = response.notification.request.content.userInfo;
if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
//应用处于后台时的远程推送接受
}else{
//应用处于后台时的本地推送接受
}
}

app未启动的情况

在 iOS9 下调用:

1
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

其中 launchOptions 中可以获取推送消息内容,然后根据消息内容进行处理。

在iOS10系统下,app接收远程推送消息分为两步:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//先调用
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{
NSDictionary * userInfo = response.notification.request.content.userInfo;
if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
//应用处于后台时的远程推送接受
//必须加这句代码
}else{
//应用处于后台时的本地推送接受
}
}

//然后 AppDelegate 中,launchOption 包含推送消息内容。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{

}

最后消息数据格式的区别

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//iOS9消息格式
{
aps = {
alert = "\U65b0\U6d88\U606f";
ound = default;
};
d = us60289150408047357911;
p = 0;
pustId = 45267;
pustType = 5;
}

//iOS消息格式
{
aps = {
alert = {
body = sdfsfsdf;
subtitle = xxxx;
title = xxx;
};
sound = default;
};
d = us29253150408072030511;
p = 0;
pustId = 45267;
pustType = 5;
}
Newer Post

swift项目架构(MVC)

目录结构├── SwiftDemo│ ├── AppDelegate.swift│ ├── Assets.xcassets│ ├── Base.lproj│ │ └── LaunchScreen.storyboard│ ├── Config│ │ ├── Const. …

note 继续阅读
Older Post

iOS内存管理笔记

1.用 alloc/new/copy/mutableCopy 以外的方法取得的对象,非自己生成并持有的对象。 2.引用计数: __strong 1234{ id __string obj = [[NSObject alloc] init]; NSLog(@"retain co …

note 继续阅读